package com.zwps.biz.domain.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwps.biz.api.enums.ProTypeEnum;
import com.zwps.biz.api.model.vo.pubpointall.DsmPrtRecordDetailVO;
import com.zwps.biz.api.model.vo.pubpointall.DsmPtrMpVO;

import com.zwps.biz.dal.db.dao.DsmPtrRecordDAO;
import com.zwps.biz.dal.db.dao.DsmPtrRecordDetailDAO;
import com.zwps.biz.dal.db.object.DsmPtrRecordDO;
import com.zwps.biz.dal.db.object.DsmPtrRecordDetailDO;
import com.zwps.biz.domain.converter.DsmPtrRecordConverter;
import com.zwps.biz.domain.service.DsmPtrRecordService;
import com.zwps.common.core.exception.UserActionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 */
@DS("qclyt")
@Service
public class DsmPtrRecordServiceImpl extends ServiceImpl<DsmPtrRecordDAO, DsmPtrRecordDO>
        implements DsmPtrRecordService {




    @Autowired
    private DsmPtrRecordDetailDAO dsmPtrRecordDetailDAO;




    @Override
    public DsmPrtRecordDetailVO getRecordDetailById(Integer id) {

        DsmPtrRecordDO dsmPtrRecordDO = this.getById(id);
        if (Objects.isNull(dsmPtrRecordDO)) {
            throw new UserActionException("工程巡查数据不存在");
        }
        DsmPrtRecordDetailVO recordDetailVO = DsmPtrRecordConverter.INSTANCE.toDsmPrtRecordDetailVO(dsmPtrRecordDO);
        List<DsmPtrRecordDetailDO> dsmPtrRecordDetailDOS = dsmPtrRecordDetailDAO.selectList(Wrappers.lambdaQuery(DsmPtrRecordDetailDO.class)
                .eq(DsmPtrRecordDetailDO::getRecordId, id));
        if (CollUtil.isEmpty(dsmPtrRecordDetailDOS)) {
            return recordDetailVO;
        }
        List<DsmPtrMpVO> detail = dsmPtrRecordDetailDAO.selectDsmPtrMpList(dsmPtrRecordDetailDOS.stream().map(DsmPtrRecordDetailDO::getMpId)
                .collect(Collectors.toList()));
        Map<Long, DsmPtrMpVO> dsmPtrMpVOMap = new HashMap<>();
        if (CollUtil.isNotEmpty(detail)) {
            dsmPtrMpVOMap = detail.stream().collect(Collectors.toMap(DsmPtrMpVO::getId, Function.identity()));
        }
        List<DsmPtrMpVO> dsmPtrMpVOS = new ArrayList<>();
        for (DsmPtrRecordDetailDO dsmPtrRecordDetailDO : dsmPtrRecordDetailDOS) {
            DsmPtrMpVO dsmPtrMpVO = dsmPtrMpVOMap.get(dsmPtrRecordDetailDO.getMpId());
            if (Objects.isNull(dsmPtrMpVO)) {
                dsmPtrMpVO = new DsmPtrMpVO();
            }
            dsmPtrMpVO.setId(dsmPtrRecordDetailDO.getId());
            dsmPtrMpVO.setRecordId(dsmPtrRecordDetailDO.getRecordId());
            dsmPtrMpVO.setMpId(dsmPtrRecordDetailDO.getMpId());
            dsmPtrMpVO.setResult(dsmPtrRecordDetailDO.getResult());
            dsmPtrMpVO.setRemark(dsmPtrRecordDetailDO.getRemark());
            dsmPtrMpVOS.add(dsmPtrMpVO);
        }
        recordDetailVO.setDetail(dsmPtrMpVOS);
        return recordDetailVO;
    }
}




